define(["jquery"],function(jq){
  jq.dateUtils=(function() {

    /**
     * 日期工具类.
     * 
     * @constructor .
     * 
     * @example $.dateUtils("format",new Date(),"YYYY-MM-DD");
     */
    function DateUtils() {
    }
    ;

    /**
     * 一天中的毫秒数(86400000).
     */
    DateUtils.MICRO_SECONDS_OF_A_DAY = 86400000;

    /**
     * 两个时间之间的时间差.
     * 
     * @param t1
     *          {Date} 被减日期.
     * @param t2
     *          {Date} 减数日期.
     * @returns {Number} t1与t2之间相差的天数.
     */
    DateUtils.diff = function(t1, t2) {
      var t1value = t1.getTime();
      var t2value = (t2 ? t2.getTime() : jq.now());
      return (t1value - t2value) / DateUtils.MICRO_SECONDS_OF_A_DAY;
    };

    var MONTH_SORT_ZHCN = [ '一', '二', '三', '四', '五', '六', '七', '八',
        '九', '十', '十一', '十二' ];
    var MONTH_LONG_ZHCN = [ '一月', '二月', '三月', '四月', '五月', '六月', '七月',
        '八月', '九月', '十月', '十一月', '十二月' ];

    var MONTH_SORT = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
        'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
    var MONTH_LONG = [ 'January', 'February', 'March', 'April', 'May',
        'June', 'July', 'August', 'September', 'October', 'November',
        'December' ];

    var WEEK_DAY_SORT = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri',
        'Sat' ];
    var WEEK_DAY_LONG = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday',
        'Thursday', 'Friday', 'Saturday' ];
    var WEEK_DAY_SORT_ZHCN = [ '日', '一', '二', '三', '四', '五', '六' ];
    var WEEK_DAY_LONG_ZHCN = [ '周日', '周一', '周二', '周三', '周四', '周五', '周六' ];

    DateUtils.format = function(date, format) {
      if (!format) {
        format = 'YYYY-MM-DD';
      }
      var res = format;
      res = res.replace(/YYYY/g, date.getFullYear());
      res = res.replace(/YY/g, date.getFullYear() % 100);
      var month = date.getMonth() + 1;

      res = res.replace(/MMMM\.zhCN/g, MONTH_LONG_ZHCN[month - 1]);
      res = res.replace(/MMM\.zhCN/g, MONTH_SORT_ZHCN[month - 1]);

      res = res.replace(/MMMM/g, MONTH_LONG[month - 1]);
      res = res.replace(/MMM/g, MONTH_SORT[month - 1]);
      res = res.replace(/MM/g, (month < 10 ? '0' : '') + month);
      res = res.replace(/M/g, month);
      var day = date.getDate();
      res = res.replace(/DD/g, (day < 10 ? '0' : '') + day);
      res = res.replace(/D/g, day);

      var weekday = date.getDay();
      res = res.replace(/ww/g, weekday);
      res = res.replace(/w/g, weekday);

      res = res.replace(/WWWW\.zhCN/g, WEEK_DAY_LONG_ZHCN[weekday]);
      res = res.replace(/WWW\.zhCN/g, WEEK_DAY_SORT_ZHCN[weekday]);

      res = res.replace(/WWWW/g, WEEK_DAY_LONG[weekday]);
      res = res.replace(/WWW/g, WEEK_DAY_SORT[weekday]);

      var hours = date.getHours();
      res = res.replace(/HH/g, (hours < 10 ? '0' : '') + hours);
      res = res.replace(/H/g, hours);
      if (hours > 12) {
        hours -= 12;
      }
      res = res.replace(/hh/g, (hours < 10 ? '0' : '') + hours);
      res = res.replace(/h/g, hours);
      var mi = date.getMinutes();
      res = res.replace(/mm/g, (mi < 10 ? '0' : '') + mi);
      res = res.replace(/m/g, mi);
      var se = date.getSeconds();
      res = res.replace(/ss/g, (se < 10 ? '0' : '') + se);
      res = res.replace(/s/g, se);
      var ms = date.getMilliseconds();
      res = res.replace(/SSS/g, (ms < 100 ? '0' : '')
          + (ms < 10 ? '0' : '') + ms);
      res = res.replace(/S/g, ms);

      return res;
    };

    DateUtils.parse = function(str) {
      var res = new Date();
      str = jq.trim(str);
      if (str.length == 10) {
        str = str + " 00:00:00.000";
      }else if(str.length == 23) {
        
      }else if (str.length > 10) {
        str = "1970-01-01 " + str;
      }
      
      console.log("str:"+str);
      
      res.setFullYear(str.substring(0, 4));
      res
          .setMonth(new Number(str.substring(5, 7).replace(/^0/, '')) - 1);
      res.setDate(new Number(str.substring(8, 10).replace(/^0/, '')));
      res.setHours(new Number(str.substring(11, 13).replace(/^0/, '')));
      res
          .setMinutes(new Number(str.substring(14, 16)
              .replace(/^0/, '')));
      res
          .setSeconds(new Number(str.substring(17, 19)
              .replace(/^0/, '')));
      res.setMilliseconds(new Number(str.substring(20, 23).replace(
          /^0/, '').replace(/^0/, '')));

      return res;
    }

    var DATE_UTILS = new DateUtils();

    /**
     * 接口函数.
     */
    var interfaceFunc = function(func) {
      var args = [];
      var len = arguments.length;
      for (var i = 1; i < len; i++) {
        args[args.length] = arguments[i];
      }
      return jq.dateUtils.METHODS[func].apply(DATE_UTILS, args);
    };

    interfaceFunc.METHODS = {
      diff : function() {
        return DateUtils.diff.apply(DATE_UTILS, arguments);
      },
      format : function() {
        return DateUtils.format.apply(DATE_UTILS, arguments);
      },
      parse : function() {
        return DateUtils.parse.apply(DATE_UTILS, arguments);
      }
    };

    return interfaceFunc;
  })();
  return jq.dateUtils;
});